package goodroommate.service.impl;

import java.util.ArrayList;

import goodroommate.dal.DatabaseSession;
import goodroommate.dao.IUniversityDao;
import goodroommate.dao.impl.UniversityDao;
import goodroommate.domain.UniversityDetail;
import goodroommate.exception.DatabaseException;
import goodroommate.exception.ItemNotExistsException;
import goodroommate.service.IUniversityService;

public class UniversityService implements IUniversityService {

	IUniversityDao universityDao=new UniversityDao();
	
	@Override
	public boolean addUniversityDetail(long universityid,String subject, String description, String profilepicture)
			throws DatabaseException {
		if(universityid==0)
			return false;
		if(subject==null)
			return false;
		if(description==null)
			return false;
		
		UniversityDetail universitydetail=new UniversityDetail();
		universitydetail.setUniversityid(universityid);
		universitydetail.setSubject(subject);
		universitydetail.setDescription(description);
		universitydetail.setProfilepicture(profilepicture);
		try(DatabaseSession dbs=new DatabaseSession()){
			universityDao.addUniversityDetail(universitydetail, dbs);
			return true;
		}
	}

	@Override
	public UniversityDetail getUniversityDetailById(long universityid) throws DatabaseException,ItemNotExistsException {
		try(DatabaseSession dbs=new DatabaseSession()){
			UniversityDetail universitydetail=universityDao.getUniversityDetailById(universityid, dbs);
			if(universitydetail==null)
				throw new ItemNotExistsException();
			else
				return universitydetail;
		}
	}

	@Override
	public ArrayList<UniversityDetail> getAllUniversityDetail() throws DatabaseException, ItemNotExistsException {

		try (DatabaseSession dbs = new DatabaseSession()) {
			ArrayList<UniversityDetail> rst = universityDao.getAllUniversityDetail(dbs);
			if (rst.size() == 0)
				throw new ItemNotExistsException();
			else
				return rst;
		}
	}
	

	
	
	

}
